package org.elasticsearch.index.analysis;

import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.settings.IndexSettingsService;
import org.lionsoul.jcseg.analyzer.JcsegTokenizer;
import org.lionsoul.jcseg.core.ADictionary;
import org.lionsoul.jcseg.core.DictionaryFactory;
import org.lionsoul.jcseg.core.JcsegTaskConfig;

import java.io.File;

/**
 * JcsegTokenizerFactory
 * 
 * @author chenxin<chenxin6193152gmail.com>
 */
public class JcsegTokenizerFactory extends AbstractTokenizerFactory 
{
    private String seg_mode;
    private JcsegTaskConfig config;
    private ADictionary dic;

    @Inject
    public JcsegTokenizerFactory(Index index, IndexSettingsService indexSettingsService,
    		Environment env, @Assisted String name, @Assisted Settings settings) 
    {
        super(index, indexSettingsService.getSettings(), name, settings);
        
        File proFile = new File(env.configFile()+"/jcseg/" + JcsegTaskConfig.LEX_CFG_FILE);
        seg_mode = settings.get("seg_mode", "complex");
        
        config = new JcsegTaskConfig(proFile.exists() ? proFile.getPath() : null);

		dic = DictionaryFactory.createDefaultDictionary(config);
    }

    @Override public Tokenizer create( )
    {
		try {
			return new JcsegTokenizer(JcsegTaskConfig.getSegMode(seg_mode), config, dic);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }
}
